Node JS 命令行工具系列

# terminalizer - 记录终端操作并生成 Gif (opens new window)

npm install terminalizer -g
terminalizer record demo
# ... you actions
terminalizer render demo

# yargs - CLI 参数解析 (opens new window)

#!/usr/bin/env node
const argv = require('yargs').argv

if (argv.ships > 3 && argv.distance < 53.5) {
  console.log('Plunder more riffiwobbles!')
} else {
  console.log('Retreat from the xupptumblers!')
}
$ ./plunder.js --ships=4 --distance=22
Plunder more riffiwobbles!

$ ./plunder.js --ships 12 --distance 98.7
Retreat from the xupptumblers!

# commander - 出自 tj 大神 (opens new window)

#!/usr/bin/env node
var program = require('commander')

program.option('--no-sauce', 'Remove sauce').parse(process.argv)

console.log('you ordered a pizza')
if (program.sauce) console.log('  with sauce')
else console.log(' without sauce')

# chalk - 多彩 log (opens new window)

const chalk = require('chalk')

console.log(chalk.blue('Hello world!'))

# colors - 多彩 log (opens new window)

var colors = require('colors')

console.log('hello'.green) // outputs green text
console.log('i like cake and pies'.underline.red) // outputs red underlined text
console.log('inverse the color'.inverse) // inverses the color
console.log('OMG Rainbows!'.rainbow) // rainbow
console.log('Run the trap'.trap) // Drops the bass

# inquirer - 交互式 CLI (opens new window)

var inquirer = require('inquirer')
inquirer
  .prompt([
    /* Pass your questions in here */
  ])
  .then((answers) => {
    // Use user feedback for... whatever!!
  })

# oclif - 命令行工具脚手架 (opens new window)

$ npx oclif single mynewcli
? npm package name (mynewcli): mynewcli
$ cd mynewcli
$ ./bin/run
hello world from ./src/index.js!